﻿$(function () {
    var projectName = $('#projectName').attr('project');
    var customerPhone_add = $('#customerPhone_add');//客户号码

    /*设置添加客户的生日格式*/
    $('.customerBirthday_format').datetimepicker({
        format: 'YYYY/MM/DD'
    });

    /*添加电话号码离交事件*/
    $('#customerPhone_add').blur(function () {
        var customerPhone = customerPhone_add.val();
        $.getJSON(
            projectName + '/customer/getCustomerPhoneById.do',
            {customerPhone: customerPhone},
            function (json) {
                if(json.code==200){
                    $('#error').text("该号码已存在，请修改");
                }else {
                    $('#error').text("");
                }
            },"json");
    });

    /*修改按钮*/
    $('.editClick').click(function () {
        var valueId = $(this).attr('v');
        console.log(valueId);
        $('#customerId_edit').val(valueId);

        $.getJSON(
            projectName + '/customer/getCustomerById.do',
            {customerId: valueId},
            function (json) {
                var customer = json.data;
                console.log(customer);
                $('#customerName_edit').val(customer.customerName);
                $('#customerPhone_edit').val(customer.customerPhone);
                var sex = customer.customerSex;
                if ("男" == sex){
                    $('#customerSex_editF').parent().removeClass("checked");
                    $('#customerSex_editM').parent().addClass("checked");
                } else if ("女" == sex){
                    $('#customerSex_editM').parent().removeClass("checked");
                    $('#customerSex_editF').parent().addClass("checked");
                }
                const type = customer.customerType;
                if(type==1){
                    $('[name="customerType"] option[value="1"]').attr("selected","selected");
                    $('[name="customerType"] option[value="2"]').removeAttr("selected");
                }else {
                    $('[name="customerType"] option[value="2"]').attr("selected","selected");
                    $('[name="customerType"] option[value="1"]').removeAttr("selected");
                }
                const birthday = myFunction(customer.customerBirthday);
                $('#customerBirthday_edit').val(birthday);
            });
    });

    /*删除按钮*/
    $('.deleteClick').click(function () {
        var customerId = $(this).attr('v');
        console.log(customerId);
        $('#delModelSubmit').attr('v',customerId);
    });

    /*确认删除按钮*/
    $('#delModelSubmit').click(function () {
        var customerId = $(this).attr('v');
        var empId = $(this).attr('empId');
        location.href = projectName + "/customer/deleteCustomerById.do?customerId=" + customerId
            + "&empId=" + empId;
    });

    function myFunction(date){
        var date= new Date(Date.parse(date));
        var y = date.getFullYear();
        var m = date.getMonth()+1;
        var d = date.getDate();
        return y+'/'+m+'/'+d;
    };

});

